/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */

package com.sys.midware.mongo.core.mongoOP;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sys.midware.mongo.core.MongoUtil;

/**
 * mongo查询类 <b>ClassName:</b> MongoQuery.java <br/>
 * <b>Date:</b> 2016年1月11日 下午1:27:50<br/>
 * eg. DBObject query = new
 * QueryBuilder().put(“sex”).is(“man”).and(“age”).greaterThan(20).get(); int[]
 * inList = {23,24}; DBObject query = new
 * QueryBuilder().put(“age”).notIn(inList).get(); int[] inList = {23,24};
 * DBObject query = new QueryBuilder().put(“age”).in(inList).get(); DBObject
 * query = new QueryBuilder().put(“age”).lessThanEquals(22).get();
 * 
 * @author mobing
 * @version
 */
public class MongoUpdate {
    /**
     * 需要更新的对象
     */
    private DBObject _update;

    /**
     * 当前设置的key
     */
//    private String _currentKey;

    /**
     * Creates a builder with an empty query
     */
    private MongoUpdate() {
        _update = new BasicDBObject();
    }

//    /**
//     * 空对象
//     */
//    private static class NullObject {
//    }

    @SuppressWarnings("serial")
    private static class MongoUpdateException extends RuntimeException {
        MongoUpdateException(String message) {
            super(message);
        }
    }

    /**
     * returns a new QueryBuilder
     * 
     * @return
     */
    public static MongoUpdate start() {
        return new MongoUpdate();
    }

    // /**
    // * Adds a new key to the query if not present yet.
    // * Sets this key as the current key.
    // * @param key MongoDB document key
    // * @return this
    // */
    // public MongoUpdate put(String key) {
    // _currentKey = key;
    // if(_update.get(key) == null) {
    // _update.put(_currentKey, new NullObject());
    // }
    // return this;
    // }

    // private void addOperand(String op, Object value) {
    // if(op == null) {
    // _update.put(_currentKey, value);
    // return;
    // }
    //
    // Object storedValue = _update.get(_currentKey);
    // BasicDBObject operand;
    // if(!(storedValue instanceof DBObject)) {
    // operand = new BasicDBObject();
    // _update.put(_currentKey, operand);
    // } else {
    // operand = (BasicDBObject)_update.get(_currentKey);
    // }
    // operand.put(op, value);
    // }
    /**
     * Creates a <code>DBObject</code> query to be used for the driver's find
     * operations
     * 
     * @return Returns a DBObject query instance
     * @throws RuntimeException
     *             if a key does not have a matching operand
     */
    public DBObject get() {
//        for (String key : _update.keySet()) {
//            if (_update.get(key) instanceof NullObject) {
//                throw new MongoUpdateException("No operand for key:" + key);
//            }
//        }
        return _update;
    }

    /**
     * Equivalent to an $or operand
     * 
     * @param ors
     *            the list of conditions to or together
     * @return Returns the current QueryBuilder with appended "or" operator
     */
    public MongoUpdate Set(Object object) {
        DBObject dbo = MongoUtil.convertToDBObject(object);
        if (dbo == null) {
            throw new IllegalArgumentException("arugument obj is not null");
        }

        if (!_update.keySet().contains(MongoOperators.SET)) {
            _update.put(MongoOperators.SET, dbo);
        }
        return this;
    }

    public MongoUpdate Set(String key, String value) {
        DBObject dbo = (DBObject) _update.get(MongoOperators.SET);
        if (dbo == null) {
            throw new MongoUpdateException("No set keyword:" + _update.toString());
        }

        dbo.put(key, value);
        return this;
    }
    
    public MongoUpdate Set(String key, DBObject value) {
        DBObject dbo = (DBObject) _update.get(MongoOperators.SET);
        if (dbo == null) {
            throw new MongoUpdateException("No set keyword:" + _update.toString());
        }

        dbo.put(key, value);
        return this;
    }

}
